package com.lanou3g.ptp.warning.job;

import com.alibaba.dubbo.config.annotation.Reference;
import com.lanou3g.ptp.achievement.service.DiaryService;
import com.lanou3g.ptp.entity.Student;
import com.lanou3g.ptp.entity.Task;
import com.lanou3g.ptp.entity.WarningPlan;
import com.lanou3g.ptp.sign.service.SignService;
import com.lanou3g.ptp.task.service.TaskService;
import com.lanou3g.ptp.taskstudent.service.TaskStudentService;
import com.lanou3g.ptp.warning.service.WarningService;
import org.quartz.JobExecutionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class WarningJob extends QuartzJobBean {

    @Reference
    private TaskService taskService;
    @Reference
    private TaskStudentService taskStudentService;
    @Reference
    private SignService signService;
    @Reference
    private DiaryService diaryService;
    @Autowired
    private WarningService warningService;

    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) {
        List<Task> tasks = taskService.findByStatus(Task.STATUS_RUNNING);
        for (Task task : tasks) {
            Long taskId = task.getId();
            List<WarningPlan> plans = warningService.findPlansByTaskId(taskId);
            List<Student> students = taskStudentService.findStudentsByTaskId(taskId);
            for (Student student : students) {
                warningService.warn(taskId, plans, student.getId());
            }
//            List<WarningPlan> plans = warningService.findPlansByTaskId(taskId);
//            if (plans != null && plans.size() > 0) {
//                List<Student> students = taskStudentService.findStudentsByTaskId(taskId);
//                for (WarningPlan plan : plans) {
//                    Integer type = plan.getType();
//                    for (Student student : students) {
//                        warningService.warn(plan,student.getId())
//                        Long studentId = student.getId();
//                        String warningMessage = null;
//                        if (WarningPlan.TYPE_SIGN.equals(type)) {
//                            Warning warning = new Warning();
//                            int timesInARow = signService.getTimesInARow(taskId, studentId);
//                            int timesPerWeek = signService.getTimesPerWeek(taskId, studentId);
//                            int timesInTotal = signService.getTimesInTotal(taskId, studentId);
//                            if (plan.getTimesInARow() != null && timesInARow >= plan.getTimesInARow()) {
//                                warning.setContent(String.format(Warning.CONTENT_SIGN_IN_A_ROW, timesInARow));
//                            }
//                        }
//                        if (WarningPlan.TYPE_DIARY.equals(type)) {
//
//                        }
//                    }
//                }
//            }

        }
    }
}
